package com.lft.juc01.base;

public class SailTicketsOriginal {
	public static void main(String[] args) {
		// 并发：多线程操作同一个资源类，把资源类丢入线程中。
		Ticket1 ticket = new Ticket1();
		
		// @FunctionalInterface 函数式接口，jdk1.8 lambda 表达式 (参数名) -> {代码}
		new Thread(() -> {for (int i = 0; i < 40; i++)ticket.sale();}, "A").start();
		new Thread(() -> {for (int i = 0; i < 40; i++)ticket.sale();}, "B").start();
		new Thread(() -> {for (int i = 0; i < 40; i++)ticket.sale();}, "C").start();
	}
}

/**
 * 资源类 OOP
 */
class Ticket1 {
	// 字段
	private int number = 30;
	
	// 方法:卖票的方式
	// synchronized 本质：队列，锁
	public synchronized void sale() {
		// 业务代码
		if (number > 0) {
			System.out.println(Thread.currentThread().getName() + " 卖出了 " + number-- + " 票，剩余 " + number);
		}
	}
}
